package com.tansun.easycare.ruleDimensioninfo.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.jeedev.msdp.base.web.ResponseVO;
import com.jeedev.msdp.help.service.IHelpDefExceptionService;
import com.jeedev.msdp.idata.utils.page.PageParamUtil;
import com.tansun.easycare.ruleDimensioninfo.dao.RuleDimensioninfoMapper;
import com.tansun.easycare.ruleDimensioninfo.entity.RuleDimensioninfo;
import com.tansun.easycare.ruleDimensioninfo.service.IRuleDimensioninfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

@Service("ruleDimensioninfoService")
@Transactional
@Slf4j
public class RuleDimensioninfoServiceImpl implements IRuleDimensioninfoService {

    @Autowired
    private RuleDimensioninfoMapper ruleDimensioninfoMapper;

    @Autowired
    private IHelpDefExceptionService helpDefExceptionService;

    @Override
    public ResponseVO selectById(@RequestBody Map<String, Object> params) {
        ResponseVO responseVO = new ResponseVO();
        if(null == params) {
            responseVO.setDescription("参数不能为空!");
            responseVO.setStatus(ResponseVO.ERROR);
            return responseVO;
        }
        String id = (String)params.get("id");
        responseVO.setData(ruleDimensioninfoMapper.selectById(id));
        responseVO.setStatus(ResponseVO.OK);
        return responseVO;
}

    @Override
    public ResponseVO insert(Map<String, Object> params) {
        ResponseVO responseVO = new ResponseVO();
        if(null == params) {
            responseVO.setDescription("参数不能为空!");
            responseVO.setStatus(ResponseVO.ERROR);
            return responseVO;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        RuleDimensioninfo ruleDimensioninfo = objectMapper.convertValue(params, RuleDimensioninfo.class);
        responseVO.setData(ruleDimensioninfoMapper.insert(ruleDimensioninfo));
        responseVO.setStatus(ResponseVO.OK);
        responseVO.setDescription("新增成功");
        return responseVO;
    }

    @Override
    public ResponseVO updateById(Map<String, Object> params) {
        ResponseVO responseVO = new ResponseVO();
        if(null == params) {
            responseVO.setDescription("参数不能为空!");
            responseVO.setStatus(ResponseVO.ERROR);
            return responseVO;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        RuleDimensioninfo ruleDimensioninfo = objectMapper.convertValue(params, RuleDimensioninfo.class);
        responseVO.setData(ruleDimensioninfoMapper.updateById(ruleDimensioninfo));
        responseVO.setStatus(ResponseVO.OK);
        responseVO.setDescription("修改成功");
        return responseVO;
    }

    @Override
    public ResponseVO deleteById(Map<String, Object> params) {
        ResponseVO responseVO = new ResponseVO();
        if(null == params) {
            responseVO.setDescription("参数不能为空!");
            responseVO.setStatus(ResponseVO.ERROR);
            return responseVO;
        }
        String id = (String)params.get("id");
        responseVO.setData(ruleDimensioninfoMapper.deleteById(id));
        responseVO.setStatus(ResponseVO.OK);
        responseVO.setDescription("删除成功");
        return responseVO;
    }

    @Override
    public ResponseVO findPage(Map<String, Object> params) {
        ResponseVO responseVO = new ResponseVO();
        PageHelper.startPage(PageParamUtil.getPageParam(params));
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("grid", ruleDimensioninfoMapper.findPage(params).toPageInfo());
        responseVO.setData(result);
        responseVO.setStatus(ResponseVO.OK);
        return responseVO;
    }

}
